{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0ecf4cd9",
   "metadata": {},
   "source": [
    "## 导入包并读取数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e7220862",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from scipy import stats\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')\n",
    "plt.rcParams['font.family']='SimHei'\n",
    "plt.rcParams['axes.unicode_minus']= False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 151,
   "id": "bd024f38",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>客户编号</th>\n",
       "      <th>已发货款</th>\n",
       "      <th>资产成本</th>\n",
       "      <th>贷款与资产比例</th>\n",
       "      <th>品牌</th>\n",
       "      <th>汽车销售商</th>\n",
       "      <th>车厂</th>\n",
       "      <th>出生日期</th>\n",
       "      <th>货款日期</th>\n",
       "      <th>地区</th>\n",
       "      <th>...</th>\n",
       "      <th>尚未还清有效贷款总额</th>\n",
       "      <th>已批准贷款总额</th>\n",
       "      <th>已发放贷款总额</th>\n",
       "      <th>每月还款总额</th>\n",
       "      <th>贷款与已还贷款比例</th>\n",
       "      <th>主账户还款期数</th>\n",
       "      <th>次账户还款期数</th>\n",
       "      <th>贷款与已批准贷款比例</th>\n",
       "      <th>总贷款次数与总有效贷款次数比</th>\n",
       "      <th>工作类型</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>601758.0</td>\n",
       "      <td>65532</td>\n",
       "      <td>78990.0</td>\n",
       "      <td>84.38</td>\n",
       "      <td>136.0</td>\n",
       "      <td>20490.0</td>\n",
       "      <td>45.0</td>\n",
       "      <td>1981.0</td>\n",
       "      <td>2018.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>519488.0</td>\n",
       "      <td>56759</td>\n",
       "      <td>65325.0</td>\n",
       "      <td>89.55</td>\n",
       "      <td>61.0</td>\n",
       "      <td>22778.0</td>\n",
       "      <td>86.0</td>\n",
       "      <td>1967.0</td>\n",
       "      <td>2018.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>...</td>\n",
       "      <td>2054139.0</td>\n",
       "      <td>2036500.0</td>\n",
       "      <td>2036500.0</td>\n",
       "      <td>34455.0</td>\n",
       "      <td>0.99</td>\n",
       "      <td>59.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.33</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>447579.0</td>\n",
       "      <td>58413</td>\n",
       "      <td>67960.0</td>\n",
       "      <td>89.02</td>\n",
       "      <td>5.0</td>\n",
       "      <td>15663.0</td>\n",
       "      <td>86.0</td>\n",
       "      <td>1977.0</td>\n",
       "      <td>2018.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>648134.0</td>\n",
       "      <td>72317</td>\n",
       "      <td>99750.0</td>\n",
       "      <td>73.68</td>\n",
       "      <td>76.0</td>\n",
       "      <td>17242.0</td>\n",
       "      <td>48.0</td>\n",
       "      <td>1995.0</td>\n",
       "      <td>2018.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>13813.0</td>\n",
       "      <td>13813.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>13814.00</td>\n",
       "      <td>13813.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.00</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>458210.0</td>\n",
       "      <td>50078</td>\n",
       "      <td>65450.0</td>\n",
       "      <td>79.45</td>\n",
       "      <td>146.0</td>\n",
       "      <td>14181.0</td>\n",
       "      <td>45.0</td>\n",
       "      <td>1974.0</td>\n",
       "      <td>2018.0</td>\n",
       "      <td>17.0</td>\n",
       "      <td>...</td>\n",
       "      <td>467161.0</td>\n",
       "      <td>550000.0</td>\n",
       "      <td>550000.0</td>\n",
       "      <td>12863.0</td>\n",
       "      <td>1.18</td>\n",
       "      <td>42.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.06</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 49 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       客户编号   已发货款     资产成本  贷款与资产比例     品牌    汽车销售商    车厂    出生日期    货款日期  \\\n",
       "0  601758.0  65532  78990.0    84.38  136.0  20490.0  45.0  1981.0  2018.0   \n",
       "1  519488.0  56759  65325.0    89.55   61.0  22778.0  86.0  1967.0  2018.0   \n",
       "2  447579.0  58413  67960.0    89.02    5.0  15663.0  86.0  1977.0  2018.0   \n",
       "3  648134.0  72317  99750.0    73.68   76.0  17242.0  48.0  1995.0  2018.0   \n",
       "4  458210.0  50078  65450.0    79.45  146.0  14181.0  45.0  1974.0  2018.0   \n",
       "\n",
       "     地区  ...  尚未还清有效贷款总额    已批准贷款总额    已发放贷款总额   每月还款总额  贷款与已还贷款比例  主账户还款期数  \\\n",
       "0   8.0  ...         0.0        0.0        0.0      0.0       1.00      0.0   \n",
       "1   6.0  ...   2054139.0  2036500.0  2036500.0  34455.0       0.99     59.0   \n",
       "2   9.0  ...         0.0        0.0        0.0      0.0       1.00      0.0   \n",
       "3   8.0  ...         0.0    13813.0    13813.0      0.0   13814.00  13813.0   \n",
       "4  17.0  ...    467161.0   550000.0   550000.0  12863.0       1.18     42.0   \n",
       "\n",
       "   次账户还款期数  贷款与已批准贷款比例  总贷款次数与总有效贷款次数比  工作类型  \n",
       "0      0.0         1.0            1.00   0.0  \n",
       "1      0.0         1.0            1.33   1.0  \n",
       "2      0.0         1.0            1.00   1.0  \n",
       "3      0.0         1.0            2.00   0.0  \n",
       "4      0.0         1.0            1.06   1.0  \n",
       "\n",
       "[5 rows x 49 columns]"
      ]
     },
     "execution_count": 151,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 事先在表格里修复字段明显的文字错误\n",
    "data_total = pd.read_csv('车贷违约预测 - 修正错误.csv',encoding='ansi')\n",
    "data_total.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "12f7a596",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>客户编号</th>\n",
       "      <th>已发货款</th>\n",
       "      <th>资产成本</th>\n",
       "      <th>贷款与资产比例</th>\n",
       "      <th>品牌</th>\n",
       "      <th>汽车销售商</th>\n",
       "      <th>车厂</th>\n",
       "      <th>出生日期</th>\n",
       "      <th>货款日期</th>\n",
       "      <th>地区</th>\n",
       "      <th>...</th>\n",
       "      <th>尚未还清有效贷款总额</th>\n",
       "      <th>已批准贷款总额</th>\n",
       "      <th>已发放贷款总额</th>\n",
       "      <th>每月还款总额</th>\n",
       "      <th>贷款与已还贷款比例</th>\n",
       "      <th>主账户还款期数</th>\n",
       "      <th>次账户还款期数</th>\n",
       "      <th>贷款与已批准贷款比例</th>\n",
       "      <th>总贷款次数与总有效贷款次数比</th>\n",
       "      <th>工作类型</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>601758.0</td>\n",
       "      <td>65532</td>\n",
       "      <td>78990.0</td>\n",
       "      <td>84.38</td>\n",
       "      <td>136.0</td>\n",
       "      <td>20490.0</td>\n",
       "      <td>45.0</td>\n",
       "      <td>1981.0</td>\n",
       "      <td>2018.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>519488.0</td>\n",
       "      <td>56759</td>\n",
       "      <td>65325.0</td>\n",
       "      <td>89.55</td>\n",
       "      <td>61.0</td>\n",
       "      <td>22778.0</td>\n",
       "      <td>86.0</td>\n",
       "      <td>1967.0</td>\n",
       "      <td>2018.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>...</td>\n",
       "      <td>2054139.0</td>\n",
       "      <td>2036500.0</td>\n",
       "      <td>2036500.0</td>\n",
       "      <td>34455.0</td>\n",
       "      <td>0.99</td>\n",
       "      <td>59.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.33</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>447579.0</td>\n",
       "      <td>58413</td>\n",
       "      <td>67960.0</td>\n",
       "      <td>89.02</td>\n",
       "      <td>5.0</td>\n",
       "      <td>15663.0</td>\n",
       "      <td>86.0</td>\n",
       "      <td>1977.0</td>\n",
       "      <td>2018.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>648134.0</td>\n",
       "      <td>72317</td>\n",
       "      <td>99750.0</td>\n",
       "      <td>73.68</td>\n",
       "      <td>76.0</td>\n",
       "      <td>17242.0</td>\n",
       "      <td>48.0</td>\n",
       "      <td>1995.0</td>\n",
       "      <td>2018.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>13813.0</td>\n",
       "      <td>13813.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>13814.00</td>\n",
       "      <td>13813.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.00</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>458210.0</td>\n",
       "      <td>50078</td>\n",
       "      <td>65450.0</td>\n",
       "      <td>79.45</td>\n",
       "      <td>146.0</td>\n",
       "      <td>14181.0</td>\n",
       "      <td>45.0</td>\n",
       "      <td>1974.0</td>\n",
       "      <td>2018.0</td>\n",
       "      <td>17.0</td>\n",
       "      <td>...</td>\n",
       "      <td>467161.0</td>\n",
       "      <td>550000.0</td>\n",
       "      <td>550000.0</td>\n",
       "      <td>12863.0</td>\n",
       "      <td>1.18</td>\n",
       "      <td>42.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.06</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 49 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       客户编号   已发货款     资产成本  贷款与资产比例     品牌    汽车销售商    车厂    出生日期    货款日期  \\\n",
       "0  601758.0  65532  78990.0    84.38  136.0  20490.0  45.0  1981.0  2018.0   \n",
       "1  519488.0  56759  65325.0    89.55   61.0  22778.0  86.0  1967.0  2018.0   \n",
       "2  447579.0  58413  67960.0    89.02    5.0  15663.0  86.0  1977.0  2018.0   \n",
       "3  648134.0  72317  99750.0    73.68   76.0  17242.0  48.0  1995.0  2018.0   \n",
       "4  458210.0  50078  65450.0    79.45  146.0  14181.0  45.0  1974.0  2018.0   \n",
       "\n",
       "     地区  ...  尚未还清有效贷款总额    已批准贷款总额    已发放贷款总额   每月还款总额  贷款与已还贷款比例  主账户还款期数  \\\n",
       "0   8.0  ...         0.0        0.0        0.0      0.0       1.00      0.0   \n",
       "1   6.0  ...   2054139.0  2036500.0  2036500.0  34455.0       0.99     59.0   \n",
       "2   9.0  ...         0.0        0.0        0.0      0.0       1.00      0.0   \n",
       "3   8.0  ...         0.0    13813.0    13813.0      0.0   13814.00  13813.0   \n",
       "4  17.0  ...    467161.0   550000.0   550000.0  12863.0       1.18     42.0   \n",
       "\n",
       "   次账户还款期数  贷款与已批准贷款比例  总贷款次数与总有效贷款次数比  工作类型  \n",
       "0      0.0         1.0            1.00   0.0  \n",
       "1      0.0         1.0            1.33   1.0  \n",
       "2      0.0         1.0            1.00   1.0  \n",
       "3      0.0         1.0            2.00   0.0  \n",
       "4      0.0         1.0            1.06   1.0  \n",
       "\n",
       "[5 rows x 49 columns]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# aa=data_total[(data_total['已发放贷款总额']!=0 )& (data_total['每月还款总额']==0 )]\n",
    "# aa[['已发放贷款总额','每月还款总额']]\n",
    "# 复制数据\n",
    "data_operate = data_total.copy()\n",
    "data_operate.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "38f46bb1",
   "metadata": {},
   "source": [
    "## 理解数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "08165ff5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 199719 entries, 0 to 199718\n",
      "Data columns (total 49 columns):\n",
      " #   Column          Non-Null Count   Dtype  \n",
      "---  ------          --------------   -----  \n",
      " 0   客户编号            199715 non-null  float64\n",
      " 1   已发货款            199719 non-null  int64  \n",
      " 2   资产成本            199717 non-null  float64\n",
      " 3   贷款与资产比例         199717 non-null  float64\n",
      " 4   品牌              199717 non-null  float64\n",
      " 5   汽车销售商           199717 non-null  float64\n",
      " 6   车厂              199717 non-null  float64\n",
      " 7   出生日期            199717 non-null  float64\n",
      " 8   货款日期            199717 non-null  float64\n",
      " 9   地区              199717 non-null  float64\n",
      " 10  对接员工编号          199717 non-null  float64\n",
      " 11  是否填写手机号         199717 non-null  float64\n",
      " 12  是否填写身份证         199717 non-null  float64\n",
      " 13  是否出具驾驶证         199717 non-null  float64\n",
      " 14  是否填写护照          199717 non-null  float64\n",
      " 15  信用评分            199717 non-null  float64\n",
      " 16  主账户贷款次数         199717 non-null  float64\n",
      " 17  主账户有效贷款次数       199717 non-null  float64\n",
      " 18  主账户中尚未还清有效贷款    199717 non-null  float64\n",
      " 19  主账户中已批准的贷款      199717 non-null  float64\n",
      " 20  主账户中已发放贷款       199717 non-null  float64\n",
      " 21  次账户贷款次数         199717 non-null  float64\n",
      " 22  次账户有效贷款次数       199717 non-null  float64\n",
      " 23  次账户中尚未还清有效贷款    199717 non-null  float64\n",
      " 24  次账户中已批准贷款       199717 non-null  float64\n",
      " 25  次账户中已发放贷款       199717 non-null  float64\n",
      " 26  主账户每月还款         199717 non-null  float64\n",
      " 27  次账户每月还款         199717 non-null  float64\n",
      " 28  近六个月新贷款次数       199717 non-null  float64\n",
      " 29  近六个月违约次数        199717 non-null  float64\n",
      " 30  平均贷款期限          199717 non-null  float64\n",
      " 31  第一次贷款距今时间       199717 non-null  float64\n",
      " 32  贷款查询次数          199717 non-null  float64\n",
      " 33  是否违约            199717 non-null  float64\n",
      " 34  贷款与资产比          199717 non-null  float64\n",
      " 35  贷款总次数           199717 non-null  float64\n",
      " 36  主账户无效贷款次数       199717 non-null  float64\n",
      " 37  次账户无效贷款次数       199717 non-null  float64\n",
      " 38  无效贷款总次数         199717 non-null  float64\n",
      " 39  尚未还清有效贷款总额      199717 non-null  float64\n",
      " 40  已批准贷款总额         199717 non-null  float64\n",
      " 41  已发放贷款总额         199717 non-null  float64\n",
      " 42  每月还款总额          199717 non-null  float64\n",
      " 43  贷款与已还贷款比例       199717 non-null  float64\n",
      " 44  主账户还款期数         199717 non-null  float64\n",
      " 45  次账户还款期数         199717 non-null  float64\n",
      " 46  贷款与已批准贷款比例      199717 non-null  float64\n",
      " 47  总贷款次数与总有效贷款次数比  199717 non-null  float64\n",
      " 48  工作类型            199717 non-null  float64\n",
      "dtypes: float64(48), int64(1)\n",
      "memory usage: 74.7 MB\n"
     ]
    }
   ],
   "source": [
    "data_operate.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "aed87e44",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0        197784\n",
       "1065.0          6\n",
       "1100.0          6\n",
       "50000.0         5\n",
       "1167.0          5\n",
       "            ...  \n",
       "12568.0         1\n",
       "2782.0          1\n",
       "18564.0         1\n",
       "780.0           1\n",
       "929.0           1\n",
       "Name: 次账户每月还款, Length: 1690, dtype: int64"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_operate.次账户每月还款.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "8028f046",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count    199717.000000\n",
       "mean         74.643960\n",
       "std          11.490485\n",
       "min          10.030000\n",
       "25%          68.730000\n",
       "50%          76.670000\n",
       "75%          83.590000\n",
       "max          95.000000\n",
       "Name: 贷款与资产比例, dtype: float64"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_operate.贷款与资产比例.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "b03093b3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "Int64Index: 199715 entries, 0 to 199718\n",
      "Data columns (total 49 columns):\n",
      " #   Column          Non-Null Count   Dtype  \n",
      "---  ------          --------------   -----  \n",
      " 0   客户编号            199715 non-null  float64\n",
      " 1   已发货款            199715 non-null  int64  \n",
      " 2   资产成本            199715 non-null  float64\n",
      " 3   贷款与资产比例         199715 non-null  float64\n",
      " 4   品牌              199715 non-null  float64\n",
      " 5   汽车销售商           199715 non-null  float64\n",
      " 6   车厂              199715 non-null  float64\n",
      " 7   出生日期            199715 non-null  float64\n",
      " 8   货款日期            199715 non-null  float64\n",
      " 9   地区              199715 non-null  float64\n",
      " 10  对接员工编号          199715 non-null  float64\n",
      " 11  是否填写手机号         199715 non-null  float64\n",
      " 12  是否填写身份证         199715 non-null  float64\n",
      " 13  是否出具驾驶证         199715 non-null  float64\n",
      " 14  是否填写护照          199715 non-null  float64\n",
      " 15  信用评分            199715 non-null  float64\n",
      " 16  主账户贷款次数         199715 non-null  float64\n",
      " 17  主账户有效贷款次数       199715 non-null  float64\n",
      " 18  主账户中尚未还清有效贷款    199715 non-null  float64\n",
      " 19  主账户中已批准的贷款      199715 non-null  float64\n",
      " 20  主账户中已发放贷款       199715 non-null  float64\n",
      " 21  次账户贷款次数         199715 non-null  float64\n",
      " 22  次账户有效贷款次数       199715 non-null  float64\n",
      " 23  次账户中尚未还清有效贷款    199715 non-null  float64\n",
      " 24  次账户中已批准贷款       199715 non-null  float64\n",
      " 25  次账户中已发放贷款       199715 non-null  float64\n",
      " 26  主账户每月还款         199715 non-null  float64\n",
      " 27  次账户每月还款         199715 non-null  float64\n",
      " 28  近六个月新贷款次数       199715 non-null  float64\n",
      " 29  近六个月违约次数        199715 non-null  float64\n",
      " 30  平均贷款期限          199715 non-null  float64\n",
      " 31  第一次贷款距今时间       199715 non-null  float64\n",
      " 32  贷款查询次数          199715 non-null  float64\n",
      " 33  是否违约            199715 non-null  float64\n",
      " 34  贷款与资产比          199715 non-null  float64\n",
      " 35  贷款总次数           199715 non-null  float64\n",
      " 36  主账户无效贷款次数       199715 non-null  float64\n",
      " 37  次账户无效贷款次数       199715 non-null  float64\n",
      " 38  无效贷款总次数         199715 non-null  float64\n",
      " 39  尚未还清有效贷款总额      199715 non-null  float64\n",
      " 40  已批准贷款总额         199715 non-null  float64\n",
      " 41  已发放贷款总额         199715 non-null  float64\n",
      " 42  每月还款总额          199715 non-null  float64\n",
      " 43  贷款与已还贷款比例       199715 non-null  float64\n",
      " 44  主账户还款期数         199715 non-null  float64\n",
      " 45  次账户还款期数         199715 non-null  float64\n",
      " 46  贷款与已批准贷款比例      199715 non-null  float64\n",
      " 47  总贷款次数与总有效贷款次数比  199715 non-null  float64\n",
      " 48  工作类型            199715 non-null  float64\n",
      "dtypes: float64(48), int64(1)\n",
      "memory usage: 76.2 MB\n"
     ]
    }
   ],
   "source": [
    "# 客户编号中存在4个空值，由于数据总量近20万，4个空值占总体的比例很低，可以直接删除。\n",
    "data_operate.dropna(axis=0,inplace=True)\n",
    "data_operate.info()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0776fece",
   "metadata": {},
   "source": [
    "### 新建衍生字段"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "id": "305aa34b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>客户编号</th>\n",
       "      <th>已发货款</th>\n",
       "      <th>资产成本</th>\n",
       "      <th>贷款与资产比例</th>\n",
       "      <th>品牌</th>\n",
       "      <th>汽车销售商</th>\n",
       "      <th>车厂</th>\n",
       "      <th>出生日期</th>\n",
       "      <th>货款日期</th>\n",
       "      <th>地区</th>\n",
       "      <th>...</th>\n",
       "      <th>主账户还款期数</th>\n",
       "      <th>次账户还款期数</th>\n",
       "      <th>贷款与已批准贷款比例</th>\n",
       "      <th>总贷款次数与总有效贷款次数比</th>\n",
       "      <th>工作类型</th>\n",
       "      <th>贷款时年龄</th>\n",
       "      <th>是否满足用户信息准入</th>\n",
       "      <th>总贷款金额与资产比</th>\n",
       "      <th>车贷所占总贷款金额比例</th>\n",
       "      <th>其他贷款已还占比</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>601758.0</td>\n",
       "      <td>65532</td>\n",
       "      <td>78990.0</td>\n",
       "      <td>84.38</td>\n",
       "      <td>136.0</td>\n",
       "      <td>20490.0</td>\n",
       "      <td>45.0</td>\n",
       "      <td>1981.0</td>\n",
       "      <td>2018.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>37.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.829624</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>519488.0</td>\n",
       "      <td>56759</td>\n",
       "      <td>65325.0</td>\n",
       "      <td>89.55</td>\n",
       "      <td>61.0</td>\n",
       "      <td>22778.0</td>\n",
       "      <td>86.0</td>\n",
       "      <td>1967.0</td>\n",
       "      <td>2018.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>...</td>\n",
       "      <td>59.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.33</td>\n",
       "      <td>1.0</td>\n",
       "      <td>51.0</td>\n",
       "      <td>1</td>\n",
       "      <td>32.043766</td>\n",
       "      <td>0.027115</td>\n",
       "      <td>-0.008661</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>447579.0</td>\n",
       "      <td>58413</td>\n",
       "      <td>67960.0</td>\n",
       "      <td>89.02</td>\n",
       "      <td>5.0</td>\n",
       "      <td>15663.0</td>\n",
       "      <td>86.0</td>\n",
       "      <td>1977.0</td>\n",
       "      <td>2018.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.0</td>\n",
       "      <td>41.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.859520</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>648134.0</td>\n",
       "      <td>72317</td>\n",
       "      <td>99750.0</td>\n",
       "      <td>73.68</td>\n",
       "      <td>76.0</td>\n",
       "      <td>17242.0</td>\n",
       "      <td>48.0</td>\n",
       "      <td>1995.0</td>\n",
       "      <td>2018.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>...</td>\n",
       "      <td>13813.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>23.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.863459</td>\n",
       "      <td>0.839626</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>458210.0</td>\n",
       "      <td>50078</td>\n",
       "      <td>65450.0</td>\n",
       "      <td>79.45</td>\n",
       "      <td>146.0</td>\n",
       "      <td>14181.0</td>\n",
       "      <td>45.0</td>\n",
       "      <td>1974.0</td>\n",
       "      <td>2018.0</td>\n",
       "      <td>17.0</td>\n",
       "      <td>...</td>\n",
       "      <td>42.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.06</td>\n",
       "      <td>1.0</td>\n",
       "      <td>44.0</td>\n",
       "      <td>1</td>\n",
       "      <td>9.168495</td>\n",
       "      <td>0.083452</td>\n",
       "      <td>0.150618</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 54 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       客户编号   已发货款     资产成本  贷款与资产比例     品牌    汽车销售商    车厂    出生日期    货款日期  \\\n",
       "0  601758.0  65532  78990.0    84.38  136.0  20490.0  45.0  1981.0  2018.0   \n",
       "1  519488.0  56759  65325.0    89.55   61.0  22778.0  86.0  1967.0  2018.0   \n",
       "2  447579.0  58413  67960.0    89.02    5.0  15663.0  86.0  1977.0  2018.0   \n",
       "3  648134.0  72317  99750.0    73.68   76.0  17242.0  48.0  1995.0  2018.0   \n",
       "4  458210.0  50078  65450.0    79.45  146.0  14181.0  45.0  1974.0  2018.0   \n",
       "\n",
       "     地区  ...  主账户还款期数  次账户还款期数  贷款与已批准贷款比例  总贷款次数与总有效贷款次数比  工作类型  贷款时年龄  \\\n",
       "0   8.0  ...      0.0      0.0         1.0            1.00   0.0   37.0   \n",
       "1   6.0  ...     59.0      0.0         1.0            1.33   1.0   51.0   \n",
       "2   9.0  ...      0.0      0.0         1.0            1.00   1.0   41.0   \n",
       "3   8.0  ...  13813.0      0.0         1.0            2.00   0.0   23.0   \n",
       "4  17.0  ...     42.0      0.0         1.0            1.06   1.0   44.0   \n",
       "\n",
       "   是否满足用户信息准入  总贷款金额与资产比  车贷所占总贷款金额比例  其他贷款已还占比  \n",
       "0           1   0.829624     1.000000  1.000000  \n",
       "1           1  32.043766     0.027115 -0.008661  \n",
       "2           1   0.859520     1.000000  1.000000  \n",
       "3           1   0.863459     0.839626  1.000000  \n",
       "4           1   9.168495     0.083452  0.150618  \n",
       "\n",
       "[5 rows x 54 columns]"
      ]
     },
     "execution_count": 127,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# data_handle01 = \n",
    "data_operate['贷款时年龄'] = data_operate['货款日期']-data_operate['出生日期']\n",
    "data_operate['是否满足用户信息准入'] = (data_operate['是否填写手机号']==1)& (data_operate['是否填写身份证']==1)\n",
    "data_operate['总贷款金额与资产比'] = (data_operate['已发货款']+data_operate['已发放贷款总额'])/data_operate['资产成本']\n",
    "data_operate['车贷所占总贷款金额比例'] =data_operate['已发货款']/(data_operate['已发货款']+data_operate['已发放贷款总额'])\n",
    "data_operate['其他贷款已还占比'] = (data_operate['已发放贷款总额']+1-data_operate['尚未还清有效贷款总额'])/(data_operate['已发放贷款总额']+1)\n",
    "data_operate['是否满足用户信息准入']=data_operate['是否满足用户信息准入'].astype('int')\n",
    "data_operate01_compute=data_operate\n",
    "data_operate01_compute.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "227a0999",
   "metadata": {},
   "source": [
    "### 删除无用字段"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "id": "97c63276",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>是否违约</th>\n",
       "      <th>车贷已发贷款</th>\n",
       "      <th>资产成本</th>\n",
       "      <th>是否填写手机号</th>\n",
       "      <th>是否填写身份证</th>\n",
       "      <th>信用评分</th>\n",
       "      <th>近六个月新贷款次数</th>\n",
       "      <th>平均贷款期限</th>\n",
       "      <th>第一次贷款距今时间</th>\n",
       "      <th>贷款查询次数</th>\n",
       "      <th>...</th>\n",
       "      <th>已发放贷款总额</th>\n",
       "      <th>每月还款总额</th>\n",
       "      <th>贷款与已批准贷款比例</th>\n",
       "      <th>总贷款次数与总有效贷款次数比</th>\n",
       "      <th>工作类型</th>\n",
       "      <th>贷款时年龄</th>\n",
       "      <th>是否满足用户信息准入</th>\n",
       "      <th>总贷款金额与资产比</th>\n",
       "      <th>车贷所占总贷款金额比例</th>\n",
       "      <th>其他贷款已还占比</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.0</td>\n",
       "      <td>65532</td>\n",
       "      <td>78990.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>37.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.829624</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.0</td>\n",
       "      <td>56759</td>\n",
       "      <td>65325.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>300.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>27.0</td>\n",
       "      <td>64.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>2036500.0</td>\n",
       "      <td>34455.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.33</td>\n",
       "      <td>1.0</td>\n",
       "      <td>51.0</td>\n",
       "      <td>1</td>\n",
       "      <td>32.043766</td>\n",
       "      <td>0.027115</td>\n",
       "      <td>-0.008661</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.0</td>\n",
       "      <td>58413</td>\n",
       "      <td>67960.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.0</td>\n",
       "      <td>41.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.859520</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.0</td>\n",
       "      <td>72317</td>\n",
       "      <td>99750.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>763.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>25.0</td>\n",
       "      <td>25.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>13813.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>23.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.863459</td>\n",
       "      <td>0.839626</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1.0</td>\n",
       "      <td>50078</td>\n",
       "      <td>65450.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>379.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>16.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>550000.0</td>\n",
       "      <td>12863.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.06</td>\n",
       "      <td>1.0</td>\n",
       "      <td>44.0</td>\n",
       "      <td>1</td>\n",
       "      <td>9.168495</td>\n",
       "      <td>0.083452</td>\n",
       "      <td>0.150618</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 25 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   是否违约  车贷已发贷款     资产成本  是否填写手机号  是否填写身份证   信用评分  近六个月新贷款次数  平均贷款期限  \\\n",
       "0   1.0   65532  78990.0      1.0      1.0    0.0        0.0     0.0   \n",
       "1   1.0   56759  65325.0      1.0      1.0  300.0        0.0    27.0   \n",
       "2   1.0   58413  67960.0      1.0      1.0    0.0        0.0     0.0   \n",
       "3   1.0   72317  99750.0      1.0      1.0  763.0        0.0    25.0   \n",
       "4   1.0   50078  65450.0      1.0      1.0  379.0        0.0     4.0   \n",
       "\n",
       "   第一次贷款距今时间  贷款查询次数  ...    已发放贷款总额   每月还款总额  贷款与已批准贷款比例  总贷款次数与总有效贷款次数比  \\\n",
       "0        0.0     0.0  ...        0.0      0.0         1.0            1.00   \n",
       "1       64.0     0.0  ...  2036500.0  34455.0         1.0            1.33   \n",
       "2        0.0     0.0  ...        0.0      0.0         1.0            1.00   \n",
       "3       25.0     0.0  ...    13813.0      0.0         1.0            2.00   \n",
       "4       16.0     0.0  ...   550000.0  12863.0         1.0            1.06   \n",
       "\n",
       "   工作类型  贷款时年龄  是否满足用户信息准入  总贷款金额与资产比  车贷所占总贷款金额比例  其他贷款已还占比  \n",
       "0   0.0   37.0           1   0.829624     1.000000  1.000000  \n",
       "1   1.0   51.0           1  32.043766     0.027115 -0.008661  \n",
       "2   1.0   41.0           1   0.859520     1.000000  1.000000  \n",
       "3   0.0   23.0           1   0.863459     0.839626  1.000000  \n",
       "4   1.0   44.0           1   9.168495     0.083452  0.150618  \n",
       "\n",
       "[5 rows x 25 columns]"
      ]
     },
     "execution_count": 128,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 删除无用字段\n",
    "data_operate02_drop =data_operate01_compute[['是否违约',\n",
    "                                            '已发货款',\n",
    "                                            '资产成本',\n",
    "                                            '是否填写手机号',\n",
    "                                            '是否填写身份证',\n",
    "                                            '信用评分',\n",
    "                                            '近六个月新贷款次数',\n",
    "                                            '平均贷款期限',\n",
    "                                            '第一次贷款距今时间',\n",
    "                                            '贷款查询次数',\n",
    "                                            '贷款与资产比',\n",
    "                                            '贷款总次数',\n",
    "                                            '无效贷款总次数',\n",
    "                                            '尚未还清有效贷款总额',\n",
    "                                            '已批准贷款总额',\n",
    "                                            '已发放贷款总额',\n",
    "                                            '每月还款总额',\n",
    "                                            '贷款与已批准贷款比例',\n",
    "                                            '总贷款次数与总有效贷款次数比',\n",
    "                                            '工作类型',\n",
    "                                            '贷款时年龄',\n",
    "                                            '是否满足用户信息准入',\n",
    "                                            '总贷款金额与资产比',\n",
    "                                            '车贷所占总贷款金额比例',\n",
    "                                            '其他贷款已还占比']]\n",
    "data_operate02_drop.rename(columns={'已发货款':'车贷已发贷款'}, inplace=True)\n",
    "data_operate02_drop.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "efa9c5f4",
   "metadata": {},
   "source": [
    "### 替换信用评分的0值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "id": "0b94ca7e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0      99690\n",
       "738.0     7419\n",
       "300.0     7354\n",
       "825.0     6402\n",
       "15.0      3192\n",
       "         ...  \n",
       "837.0        1\n",
       "868.0        1\n",
       "822.0        1\n",
       "867.0        1\n",
       "863.0        1\n",
       "Name: 信用评分, Length: 572, dtype: int64"
      ]
     },
     "execution_count": 129,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# data_operate02_drop[data_operate02_drop['信用评分']!=0]['信用评分'].value_counts()\n",
    "data_operate02_drop['信用评分'].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "id": "99750141",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "300.0    107044\n",
       "738.0      7419\n",
       "825.0      6402\n",
       "15.0       3192\n",
       "17.0       3162\n",
       "          ...  \n",
       "837.0         1\n",
       "868.0         1\n",
       "822.0         1\n",
       "867.0         1\n",
       "863.0         1\n",
       "Name: 信用评分, Length: 571, dtype: int64"
      ]
     },
     "execution_count": 130,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 信用评分下降到0的概率很低，数据中出现空值的情况最大可能的原因是系统没有给该用户评定信用分。在进行预测时，给0分的用户赋予初始分数。\n",
    "# 由于初始分数一般为50或者100的整数倍，所以通过观察评分的频数分布，暂定300分为初始分。\n",
    "# data_operate03_clean = \n",
    "data_operate02_drop.loc[data_operate02_drop['信用评分']==0,'信用评分']=300\n",
    "data_operate03_clean=data_operate02_drop\n",
    "data_operate03_clean['信用评分'].value_counts()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4efb57e0",
   "metadata": {},
   "source": [
    "### 尚未还清有效贷款总额异常值处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "id": "c583ecaa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>是否违约</th>\n",
       "      <th>车贷已发贷款</th>\n",
       "      <th>资产成本</th>\n",
       "      <th>是否填写手机号</th>\n",
       "      <th>是否填写身份证</th>\n",
       "      <th>信用评分</th>\n",
       "      <th>近六个月新贷款次数</th>\n",
       "      <th>平均贷款期限</th>\n",
       "      <th>第一次贷款距今时间</th>\n",
       "      <th>贷款查询次数</th>\n",
       "      <th>...</th>\n",
       "      <th>已发放贷款总额</th>\n",
       "      <th>每月还款总额</th>\n",
       "      <th>贷款与已批准贷款比例</th>\n",
       "      <th>总贷款次数与总有效贷款次数比</th>\n",
       "      <th>工作类型</th>\n",
       "      <th>贷款时年龄</th>\n",
       "      <th>是否满足用户信息准入</th>\n",
       "      <th>总贷款金额与资产比</th>\n",
       "      <th>车贷所占总贷款金额比例</th>\n",
       "      <th>其他贷款已还占比</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.0</td>\n",
       "      <td>65532</td>\n",
       "      <td>78990.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>300.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>37.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.829624</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.0</td>\n",
       "      <td>56759</td>\n",
       "      <td>65325.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>300.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>27.0</td>\n",
       "      <td>64.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>2036500.0</td>\n",
       "      <td>34455.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.33</td>\n",
       "      <td>1.0</td>\n",
       "      <td>51.0</td>\n",
       "      <td>1</td>\n",
       "      <td>32.043766</td>\n",
       "      <td>0.027115</td>\n",
       "      <td>-0.008661</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.0</td>\n",
       "      <td>58413</td>\n",
       "      <td>67960.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>300.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.0</td>\n",
       "      <td>41.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.859520</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.0</td>\n",
       "      <td>72317</td>\n",
       "      <td>99750.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>763.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>25.0</td>\n",
       "      <td>25.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>13813.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>23.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.863459</td>\n",
       "      <td>0.839626</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1.0</td>\n",
       "      <td>50078</td>\n",
       "      <td>65450.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>379.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>16.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>550000.0</td>\n",
       "      <td>12863.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.06</td>\n",
       "      <td>1.0</td>\n",
       "      <td>44.0</td>\n",
       "      <td>1</td>\n",
       "      <td>9.168495</td>\n",
       "      <td>0.083452</td>\n",
       "      <td>0.150618</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>199204</th>\n",
       "      <td>0.0</td>\n",
       "      <td>36439</td>\n",
       "      <td>60424.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>753.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>53.0</td>\n",
       "      <td>85.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>525000.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>32.0</td>\n",
       "      <td>1</td>\n",
       "      <td>9.291656</td>\n",
       "      <td>0.064903</td>\n",
       "      <td>-0.128889</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>199205</th>\n",
       "      <td>0.0</td>\n",
       "      <td>52303</td>\n",
       "      <td>72677.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>300.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>33.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.719664</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>199206</th>\n",
       "      <td>0.0</td>\n",
       "      <td>54413</td>\n",
       "      <td>62710.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>771.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>1220000.0</td>\n",
       "      <td>2500.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.00</td>\n",
       "      <td>1.0</td>\n",
       "      <td>45.0</td>\n",
       "      <td>1</td>\n",
       "      <td>20.322325</td>\n",
       "      <td>0.042697</td>\n",
       "      <td>0.028197</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>199207</th>\n",
       "      <td>0.0</td>\n",
       "      <td>54509</td>\n",
       "      <td>71921.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>300.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.0</td>\n",
       "      <td>35.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.757901</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>199208</th>\n",
       "      <td>0.0</td>\n",
       "      <td>63147</td>\n",
       "      <td>72000.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>708.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>106508.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.00</td>\n",
       "      <td>2.0</td>\n",
       "      <td>48.0</td>\n",
       "      <td>1</td>\n",
       "      <td>2.356319</td>\n",
       "      <td>0.372208</td>\n",
       "      <td>0.080237</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>199209 rows × 25 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        是否违约  车贷已发贷款     资产成本  是否填写手机号  是否填写身份证   信用评分  近六个月新贷款次数  平均贷款期限  \\\n",
       "0        1.0   65532  78990.0      1.0      1.0  300.0        0.0     0.0   \n",
       "1        1.0   56759  65325.0      1.0      1.0  300.0        0.0    27.0   \n",
       "2        1.0   58413  67960.0      1.0      1.0  300.0        0.0     0.0   \n",
       "3        1.0   72317  99750.0      1.0      1.0  763.0        0.0    25.0   \n",
       "4        1.0   50078  65450.0      1.0      1.0  379.0        0.0     4.0   \n",
       "...      ...     ...      ...      ...      ...    ...        ...     ...   \n",
       "199204   0.0   36439  60424.0      1.0      1.0  753.0        0.0    53.0   \n",
       "199205   0.0   52303  72677.0      1.0      1.0  300.0        0.0     0.0   \n",
       "199206   0.0   54413  62710.0      1.0      1.0  771.0        1.0     7.0   \n",
       "199207   0.0   54509  71921.0      1.0      1.0  300.0        0.0     0.0   \n",
       "199208   0.0   63147  72000.0      1.0      1.0  708.0        1.0     6.0   \n",
       "\n",
       "        第一次贷款距今时间  贷款查询次数  ...    已发放贷款总额   每月还款总额  贷款与已批准贷款比例  \\\n",
       "0             0.0     0.0  ...        0.0      0.0         1.0   \n",
       "1            64.0     0.0  ...  2036500.0  34455.0         1.0   \n",
       "2             0.0     0.0  ...        0.0      0.0         1.0   \n",
       "3            25.0     0.0  ...    13813.0      0.0         1.0   \n",
       "4            16.0     0.0  ...   550000.0  12863.0         1.0   \n",
       "...           ...     ...  ...        ...      ...         ...   \n",
       "199204       85.0     0.0  ...   525000.0      0.0         1.0   \n",
       "199205        0.0     0.0  ...        0.0      0.0         1.0   \n",
       "199206        1.0     0.0  ...  1220000.0   2500.0         1.0   \n",
       "199207        0.0     0.0  ...        0.0      0.0         1.0   \n",
       "199208        1.0     0.0  ...   106508.0      0.0         1.0   \n",
       "\n",
       "        总贷款次数与总有效贷款次数比  工作类型  贷款时年龄  是否满足用户信息准入  总贷款金额与资产比  车贷所占总贷款金额比例  \\\n",
       "0                 1.00   0.0   37.0           1   0.829624     1.000000   \n",
       "1                 1.33   1.0   51.0           1  32.043766     0.027115   \n",
       "2                 1.00   1.0   41.0           1   0.859520     1.000000   \n",
       "3                 2.00   0.0   23.0           1   0.863459     0.839626   \n",
       "4                 1.06   1.0   44.0           1   9.168495     0.083452   \n",
       "...                ...   ...    ...         ...        ...          ...   \n",
       "199204            3.00   0.0   32.0           1   9.291656     0.064903   \n",
       "199205            1.00   0.0   33.0           1   0.719664     1.000000   \n",
       "199206            3.00   1.0   45.0           1  20.322325     0.042697   \n",
       "199207            1.00   1.0   35.0           1   0.757901     1.000000   \n",
       "199208            2.00   2.0   48.0           1   2.356319     0.372208   \n",
       "\n",
       "        其他贷款已还占比  \n",
       "0       1.000000  \n",
       "1      -0.008661  \n",
       "2       1.000000  \n",
       "3       1.000000  \n",
       "4       0.150618  \n",
       "...          ...  \n",
       "199204 -0.128889  \n",
       "199205  1.000000  \n",
       "199206  0.028197  \n",
       "199207  1.000000  \n",
       "199208  0.080237  \n",
       "\n",
       "[199209 rows x 25 columns]"
      ]
     },
     "execution_count": 131,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cancel_data01 = data_operate03_clean[(data_operate03_clean['已批准贷款总额']==0) \\\n",
    "                     & (data_operate03_clean['已发放贷款总额']==0)\\\n",
    "                     & (data_operate03_clean['尚未还清有效贷款总额']!=0)]\n",
    "data_operate03_clean.drop(cancel_data01.index,inplace=True)\n",
    "data_operate03_clean.reset_index(drop=True, inplace=True)\n",
    "data_operate03_clean"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a856fc13",
   "metadata": {},
   "source": [
    "### 已批准贷款总额、已发放贷款总额异常值处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "id": "0793943c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>已批准贷款总额</th>\n",
       "      <th>已发放贷款总额</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2036500.0</td>\n",
       "      <td>2036500.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>13813.0</td>\n",
       "      <td>13813.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>550000.0</td>\n",
       "      <td>550000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>199204</th>\n",
       "      <td>525000.0</td>\n",
       "      <td>525000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>199205</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>199206</th>\n",
       "      <td>1220000.0</td>\n",
       "      <td>1220000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>199207</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>199208</th>\n",
       "      <td>106508.0</td>\n",
       "      <td>106508.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>199209 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "          已批准贷款总额    已发放贷款总额\n",
       "0             0.0        0.0\n",
       "1       2036500.0  2036500.0\n",
       "2             0.0        0.0\n",
       "3         13813.0    13813.0\n",
       "4        550000.0   550000.0\n",
       "...           ...        ...\n",
       "199204   525000.0   525000.0\n",
       "199205        0.0        0.0\n",
       "199206  1220000.0  1220000.0\n",
       "199207        0.0        0.0\n",
       "199208   106508.0   106508.0\n",
       "\n",
       "[199209 rows x 2 columns]"
      ]
     },
     "execution_count": 132,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cancel_data02 = data_operate03_clean[(data_operate03_clean['已批准贷款总额']==0) \\\n",
    "                     & (data_operate03_clean['已发放贷款总额']!=0)]\n",
    "cancel_data02['已批准贷款总额'] = cancel_data02['已发放贷款总额']\n",
    "data_operate03_clean.loc[cancel_data02.index,:]=cancel_data02\n",
    "\n",
    "cancel_data03 = data_operate03_clean[(data_operate03_clean['已发放贷款总额']==0) \\\n",
    "                     & (data_operate03_clean['已批准贷款总额']!=0)]\n",
    "cancel_data03['已发放贷款总额'] = cancel_data03['已批准贷款总额']\n",
    "data_operate03_clean.loc[cancel_data03.index,:]=cancel_data03\n",
    "\n",
    "data_operate03_clean[['已批准贷款总额','已发放贷款总额']] "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6f8110da",
   "metadata": {},
   "source": [
    "### 每月还款总额异常值处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "id": "cac4b631",
   "metadata": {},
   "outputs": [],
   "source": [
    "cancel_data03 = data_operate03_clean[(data_operate03_clean['尚未还清有效贷款总额']==0)\n",
    "                     &(data_operate03_clean['已批准贷款总额']==0) \\\n",
    "                     & (data_operate03_clean['已发放贷款总额']==0)\\\n",
    "                     & (data_operate03_clean['每月还款总额']!=0)]\n",
    "data_operate03_clean.drop(cancel_data03.index,inplace=True)\n",
    "data_operate03_clean.reset_index(drop=True, inplace=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "13bfa46d",
   "metadata": {},
   "source": [
    "### 其他贷款已还占比空值处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "id": "2ce514ae",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<bound method Series.isna of 0         1.000000\n",
       "1        -0.008661\n",
       "2         1.000000\n",
       "3         1.000000\n",
       "4         0.150618\n",
       "            ...   \n",
       "190329   -0.128889\n",
       "190330    1.000000\n",
       "190331    0.028197\n",
       "190332    1.000000\n",
       "190333    0.080237\n",
       "Name: 其他贷款已还占比, Length: 190334, dtype: float64>"
      ]
     },
     "execution_count": 134,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_operate03_clean['其他贷款已还占比'].fillna(1,inplace=True)\n",
    "data_operate03_clean['其他贷款已还占比'].isna"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "id": "4526f649",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 190334 entries, 0 to 190333\n",
      "Data columns (total 25 columns):\n",
      " #   Column          Non-Null Count   Dtype  \n",
      "---  ------          --------------   -----  \n",
      " 0   是否违约            190334 non-null  float64\n",
      " 1   车贷已发贷款          190334 non-null  int64  \n",
      " 2   资产成本            190334 non-null  float64\n",
      " 3   是否填写手机号         190334 non-null  float64\n",
      " 4   是否填写身份证         190334 non-null  float64\n",
      " 5   信用评分            190334 non-null  float64\n",
      " 6   近六个月新贷款次数       190334 non-null  float64\n",
      " 7   平均贷款期限          190334 non-null  float64\n",
      " 8   第一次贷款距今时间       190334 non-null  float64\n",
      " 9   贷款查询次数          190334 non-null  float64\n",
      " 10  贷款与资产比          190334 non-null  float64\n",
      " 11  贷款总次数           190334 non-null  float64\n",
      " 12  无效贷款总次数         190334 non-null  float64\n",
      " 13  尚未还清有效贷款总额      190334 non-null  float64\n",
      " 14  已批准贷款总额         190334 non-null  float64\n",
      " 15  已发放贷款总额         190334 non-null  float64\n",
      " 16  每月还款总额          190334 non-null  float64\n",
      " 17  贷款与已批准贷款比例      190334 non-null  float64\n",
      " 18  总贷款次数与总有效贷款次数比  190334 non-null  float64\n",
      " 19  工作类型            190334 non-null  float64\n",
      " 20  贷款时年龄           190334 non-null  float64\n",
      " 21  是否满足用户信息准入      190334 non-null  int32  \n",
      " 22  总贷款金额与资产比       190334 non-null  float64\n",
      " 23  车贷所占总贷款金额比例     190334 non-null  float64\n",
      " 24  其他贷款已还占比        190334 non-null  float64\n",
      "dtypes: float64(23), int32(1), int64(1)\n",
      "memory usage: 35.6 MB\n"
     ]
    }
   ],
   "source": [
    "data_operate03_clean.info()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f6e9b19a",
   "metadata": {},
   "source": [
    "### 字段数据类型转换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "id": "38d79a59",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 190334 entries, 0 to 190333\n",
      "Data columns (total 25 columns):\n",
      " #   Column          Non-Null Count   Dtype  \n",
      "---  ------          --------------   -----  \n",
      " 0   是否违约            190334 non-null  float64\n",
      " 1   车贷已发贷款          190334 non-null  float64\n",
      " 2   资产成本            190334 non-null  float64\n",
      " 3   是否填写手机号         190334 non-null  int16  \n",
      " 4   是否填写身份证         190334 non-null  int16  \n",
      " 5   信用评分            190334 non-null  int16  \n",
      " 6   近六个月新贷款次数       190334 non-null  int16  \n",
      " 7   平均贷款期限          190334 non-null  float64\n",
      " 8   第一次贷款距今时间       190334 non-null  float64\n",
      " 9   贷款查询次数          190334 non-null  int16  \n",
      " 10  贷款与资产比          190334 non-null  float64\n",
      " 11  贷款总次数           190334 non-null  int16  \n",
      " 12  无效贷款总次数         190334 non-null  int16  \n",
      " 13  尚未还清有效贷款总额      190334 non-null  float64\n",
      " 14  已批准贷款总额         190334 non-null  float64\n",
      " 15  已发放贷款总额         190334 non-null  float64\n",
      " 16  每月还款总额          190334 non-null  float64\n",
      " 17  贷款与已批准贷款比例      190334 non-null  float64\n",
      " 18  总贷款次数与总有效贷款次数比  190334 non-null  float64\n",
      " 19  工作类型            190334 non-null  int16  \n",
      " 20  贷款时年龄           190334 non-null  int16  \n",
      " 21  是否满足用户信息准入      190334 non-null  int16  \n",
      " 22  总贷款金额与资产比       190334 non-null  int16  \n",
      " 23  车贷所占总贷款金额比例     190334 non-null  float64\n",
      " 24  其他贷款已还占比        190334 non-null  float64\n",
      "dtypes: float64(14), int16(11)\n",
      "memory usage: 24.3 MB\n"
     ]
    }
   ],
   "source": [
    "# 数据类型转换\n",
    "data_operate03_clean.信用评分=data_operate03_clean.信用评分.astype('int16')\n",
    "data_operate03_clean.近六个月新贷款次数=data_operate03_clean.近六个月新贷款次数.astype('int16')\n",
    "data_operate03_clean.贷款查询次数=data_operate03_clean.贷款查询次数.astype('int16')\n",
    "data_operate03_clean.贷款总次数=data_operate03_clean.贷款总次数.astype('int16')\n",
    "data_operate03_clean.无效贷款总次数=data_operate03_clean.无效贷款总次数.astype('int16')\n",
    "data_operate03_clean.工作类型=data_operate03_clean.工作类型.astype('int16')\n",
    "data_operate03_clean.是否填写手机号=data_operate03_clean.是否填写手机号.astype('int16')\n",
    "data_operate03_clean.是否填写身份证=data_operate03_clean.是否填写身份证.astype('int16')\n",
    "data_operate03_clean.贷款时年龄=data_operate03_clean.贷款时年龄.astype('int16')\n",
    "data_operate03_clean.是否满足用户信息准入=data_operate03_clean.是否满足用户信息准入.astype('int16')\n",
    "data_operate03_clean.总贷款金额与资产比=data_operate03_clean.总贷款金额与资产比.astype('int16')\n",
    "\n",
    "\n",
    "data_operate03_clean.车贷已发贷款=data_operate03_clean.车贷已发贷款.astype('float64')\n",
    "data_operate03_clean.资产成本=data_operate03_clean.资产成本.astype('float64')\n",
    "data_operate03_clean.平均贷款期限=data_operate03_clean.平均贷款期限.astype('float64')\n",
    "data_operate03_clean.第一次贷款距今时间=data_operate03_clean.第一次贷款距今时间.astype('float64')\n",
    "data_operate03_clean.贷款与资产比=data_operate03_clean.贷款与资产比.astype('float64')\n",
    "data_operate03_clean.尚未还清有效贷款总额=data_operate03_clean.尚未还清有效贷款总额.astype('float64')\n",
    "data_operate03_clean.已批准贷款总额=data_operate03_clean.已批准贷款总额.astype('float64')\n",
    "data_operate03_clean.已发放贷款总额=data_operate03_clean.已发放贷款总额.astype('float64')\n",
    "data_operate03_clean.每月还款总额=data_operate03_clean.每月还款总额.astype('float64')\n",
    "data_operate03_clean.贷款与已批准贷款比例=data_operate03_clean.贷款与已批准贷款比例.astype('float64')\n",
    "data_operate03_clean.总贷款次数与总有效贷款次数比=data_operate03_clean.总贷款次数与总有效贷款次数比.astype('float64')\n",
    "data_operate03_clean.车贷所占总贷款金额比例=data_operate03_clean.车贷所占总贷款金额比例.astype('float64')\n",
    "data_operate03_clean.其他贷款已还占比=data_operate03_clean.其他贷款已还占比.astype('float64')\n",
    "data_operate03_clean.info()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d8956cb3",
   "metadata": {},
   "source": [
    "### 失衡数据判断并处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 143,
   "id": "8ad344ea",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0    156536\n",
       "1.0     33798\n",
       "Name: 是否违约, dtype: int64"
      ]
     },
     "execution_count": 143,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_operate03_clean.是否违约.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 146,
   "id": "3f6f42d3",
   "metadata": {
    "code_folding": [],
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32mC:\\Users\\ADMINI~1\\AppData\\Local\\Temp/ipykernel_560/567740334.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[1;31m#2. 选择离正样本平均距离最远的N个负样本\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      6\u001b[0m \u001b[1;31m#3. 两段式,先保留M个离正样本平均距离最近负样本,然后再从M个负样本中取平均距离最远的N个负样本取中间的\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 7\u001b[1;33m \u001b[0mX_resampled\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my_resampled\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mee\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfit_resample\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdata_operate03_clean\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0miloc\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdata_operate03_clean\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m是否违约\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      8\u001b[0m \u001b[0mdata_operate04_balance\u001b[0m \u001b[1;33m=\u001b[0m\u001b[0mpd\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mconcat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mX_resampled\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0my_resampled\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0maxis\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      9\u001b[0m \u001b[0mdata_operate04_balance\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mhead\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\users\\administrator\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\imblearn\\base.py\u001b[0m in \u001b[0;36mfit_resample\u001b[1;34m(self, X, y)\u001b[0m\n\u001b[0;32m     81\u001b[0m         )\n\u001b[0;32m     82\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 83\u001b[1;33m         \u001b[0moutput\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_fit_resample\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mX\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     84\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     85\u001b[0m         y_ = (\n",
      "\u001b[1;32mc:\\users\\administrator\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\imblearn\\under_sampling\\_prototype_selection\\_nearmiss.py\u001b[0m in \u001b[0;36m_fit_resample\u001b[1;34m(self, X, y)\u001b[0m\n\u001b[0;32m    220\u001b[0m                 \u001b[1;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mversion\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    221\u001b[0m                     dist_vec, idx_vec = self.nn_.kneighbors(\n\u001b[1;32m--> 222\u001b[1;33m                         \u001b[0mX_class\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mn_neighbors\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mnn_\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mn_neighbors\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    223\u001b[0m                     )\n\u001b[0;32m    224\u001b[0m                     index_target_class = self._selection_dist_based(\n",
      "\u001b[1;32mc:\\users\\administrator\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\sklearn\\neighbors\\_base.py\u001b[0m in \u001b[0;36mkneighbors\u001b[1;34m(self, X, n_neighbors, return_distance)\u001b[0m\n\u001b[0;32m    754\u001b[0m                     \u001b[0mmetric\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0meffective_metric_\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    755\u001b[0m                     \u001b[0mn_jobs\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mn_jobs\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 756\u001b[1;33m                     \u001b[1;33m**\u001b[0m\u001b[0mkwds\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    757\u001b[0m                 )\n\u001b[0;32m    758\u001b[0m             )\n",
      "\u001b[1;32mc:\\users\\administrator\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\sklearn\\metrics\\pairwise.py\u001b[0m in \u001b[0;36mpairwise_distances_chunked\u001b[1;34m(X, Y, reduce_func, metric, n_jobs, working_memory, **kwds)\u001b[0m\n\u001b[0;32m   1719\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mreduce_func\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1720\u001b[0m             \u001b[0mchunk_size\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mD_chunk\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1721\u001b[1;33m             \u001b[0mD_chunk\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mreduce_func\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mD_chunk\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msl\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mstart\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1722\u001b[0m             \u001b[0m_check_chunk_size\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mD_chunk\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mchunk_size\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1723\u001b[0m         \u001b[1;32myield\u001b[0m \u001b[0mD_chunk\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\users\\administrator\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\sklearn\\neighbors\\_base.py\u001b[0m in \u001b[0;36m_kneighbors_reduce_func\u001b[1;34m(self, dist, start, n_neighbors, return_distance)\u001b[0m\n\u001b[0;32m    629\u001b[0m         \"\"\"\n\u001b[0;32m    630\u001b[0m         \u001b[0msample_range\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0marange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdist\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 631\u001b[1;33m         \u001b[0mneigh_ind\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0margpartition\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdist\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mn_neighbors\u001b[0m \u001b[1;33m-\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    632\u001b[0m         \u001b[0mneigh_ind\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mneigh_ind\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m:\u001b[0m\u001b[0mn_neighbors\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    633\u001b[0m         \u001b[1;31m# argpartition doesn't guarantee sorted order, so we sort again\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m<__array_function__ internals>\u001b[0m in \u001b[0;36margpartition\u001b[1;34m(*args, **kwargs)\u001b[0m\n",
      "\u001b[1;32mc:\\users\\administrator\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\numpy\\core\\fromnumeric.py\u001b[0m in \u001b[0;36margpartition\u001b[1;34m(a, kth, axis, kind, order)\u001b[0m\n\u001b[0;32m    837\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    838\u001b[0m     \"\"\"\n\u001b[1;32m--> 839\u001b[1;33m     \u001b[1;32mreturn\u001b[0m \u001b[0m_wrapfunc\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'argpartition'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mkth\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0maxis\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mkind\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mkind\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0morder\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0morder\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    840\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    841\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\users\\administrator\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\numpy\\core\\fromnumeric.py\u001b[0m in \u001b[0;36m_wrapfunc\u001b[1;34m(obj, method, *args, **kwds)\u001b[0m\n\u001b[0;32m     55\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     56\u001b[0m     \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 57\u001b[1;33m         \u001b[1;32mreturn\u001b[0m \u001b[0mbound\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwds\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     58\u001b[0m     \u001b[1;32mexcept\u001b[0m \u001b[0mTypeError\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     59\u001b[0m         \u001b[1;31m# A TypeError occurs if the object does have such a method in its\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "#少类⼩于10%，我们就认为是不平衡数据了,本次少类达到20%，但是依旧进行失衡处理练习。\n",
    "from imblearn.under_sampling import NearMiss #KNN\n",
    "ee =NearMiss(version=1)\n",
    "#1. 选择离正样本平均距离最近的N个负样本\n",
    "#2. 选择离正样本平均距离最远的N个负样本\n",
    "#3. 两段式,先保留M个离正样本平均距离最近负样本,然后再从M个负样本中取平均距离最远的N个负样本取中间的\n",
    "X_resampled, y_resampled = ee.fit_resample(data_operate03_clean.iloc[:,1:], data_operate03_clean.是否违约)\n",
    "data_operate04_balance =pd.concat([X_resampled,y_resampled],axis=1)\n",
    "data_operate04_balance.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ad149f8c",
   "metadata": {},
   "source": [
    "## 建模及评估"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a9309fd3",
   "metadata": {},
   "source": [
    "### 多个备选模型比较"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "id": "f93d4090",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import time\n",
    "# 模型处理模块\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "# 标准化处理模块\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "# 常规模型\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "from sklearn.svm import SVC\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "# 集成学习和stacking模型\n",
    "from sklearn.ensemble import AdaBoostClassifier, GradientBoostingClassifier, RandomForestClassifier\n",
    "import xgboost as xgb\n",
    "from xgboost.sklearn import XGBClassifier\n",
    "from mlxtend.classifier import StackingClassifier\n",
    "# 评价标准模块\n",
    "from sklearn import metrics\n",
    "from sklearn.metrics import accuracy_score,roc_auc_score,recall_score,precision_score\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "id": "bee8bf63",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train,X_test,y_train,y_test = train_test_split(data_operate03_clean.iloc[:,1:],data_operate03_clean.是否违约,test_size=0.3,random_state=6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "id": "5f09edb1",
   "metadata": {},
   "outputs": [],
   "source": [
    "def train_model(X_train, y_train, X_test, y_test,\n",
    "               model,model_name):\n",
    "    \n",
    "    print('训练{}'.format(model_name))\n",
    "    \n",
    "    clf=model\n",
    "    start = time.time()\n",
    "    clf.fit(X_train, y_train.values.ravel())\n",
    "    \n",
    "     #验证模型\n",
    "    print('训练准确率：{:.4f}'.format(clf.score(X_train, y_train)))\n",
    "    \n",
    "    \n",
    "    predict=clf.predict(X_test)\n",
    "    score = clf.score(X_test, y_test)\n",
    "    precision=precision_score(y_test,predict)\n",
    "    recall=recall_score(y_test,predict)\n",
    "    print('测试准确率：{:.4f}'.format(score))\n",
    "    print('测试精确率：{:.4f}'.format(precision))\n",
    "    print('测试召回率：{:.4f}'.format(recall))\n",
    "    \n",
    "    end = time.time()\n",
    "    duration = end - start\n",
    "    print('模型训练耗时：{:6f}s'.format(duration))\n",
    "    \n",
    "    \n",
    "    return clf, score,precision,recall, duration"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "id": "ad3a7277",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "训练LR\n",
      "训练准确率：0.8230\n",
      "测试准确率：0.8210\n",
      "测试精确率：0.0000\n",
      "测试召回率：0.0000\n",
      "模型训练耗时：4.587474s\n",
      "训练DT\n",
      "训练准确率：0.8270\n",
      "测试准确率：0.8161\n",
      "测试精确率：0.1828\n",
      "测试召回率：0.0079\n",
      "模型训练耗时：1.750848s\n",
      "训练AdaBoost\n",
      "训练准确率：0.8230\n",
      "测试准确率：0.8210\n",
      "测试精确率：0.4783\n",
      "测试召回率：0.0011\n",
      "模型训练耗时：17.117290s\n",
      "训练GBDT\n",
      "训练准确率：0.8231\n",
      "测试准确率：0.8210\n",
      "测试精确率：0.0000\n",
      "测试召回率：0.0000\n",
      "模型训练耗时：53.195529s\n",
      "训练RF\n",
      "训练准确率：0.9995\n",
      "测试准确率：0.8131\n",
      "测试精确率：0.2772\n",
      "测试召回率：0.0277\n",
      "模型训练耗时：57.068813s\n",
      "训练XGBoost\n",
      "[21:01:51] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.5.1/src/learner.cc:1115: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'. Explicitly set eval_metric if you'd like to restore the old behavior.\n",
      "训练准确率：0.8277\n",
      "测试准确率：0.8201\n",
      "测试精确率：0.3088\n",
      "测试召回率：0.0041\n",
      "模型训练耗时：17.415155s\n"
     ]
    }
   ],
   "source": [
    "model_name_param_dict = {    'LR': (LogisticRegression(penalty =\"l2\")),\n",
    "                             'DT': (DecisionTreeClassifier(max_depth=10,min_samples_split=10)),\n",
    "                             'AdaBoost': (AdaBoostClassifier()),\n",
    "                             'GBDT': (GradientBoostingClassifier()),\n",
    "                             'RF': (RandomForestClassifier()),\n",
    "                             'XGBoost':(XGBClassifier())\n",
    "                         }\n",
    "\n",
    "result_df = pd.DataFrame(columns=['Accuracy (%)','precision(%)','recall(%)','Time (s)'],\n",
    "                             index=list(model_name_param_dict.keys()))\n",
    "\n",
    "for model_name, model in model_name_param_dict.items():\n",
    "    clf, acc,pre,recall, mean_duration = train_model(X_train, y_train,\n",
    "                                                        X_test, y_test,\n",
    "                                                        model,model_name)\n",
    "    result_df.loc[model_name, 'Accuracy (%)'] = acc\n",
    "    result_df.loc[model_name, 'precision(%)'] = pre\n",
    "    result_df.loc[model_name, 'recall(%)'] = recall\n",
    "    result_df.loc[model_name, 'Time (s)'] = mean_duration \n",
    "\n",
    "result_df.to_csv(os.path.join('model_comparison.csv'))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cf5d0a75",
   "metadata": {},
   "source": [
    "### 利用网格搜索调优"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 142,
   "id": "37cc1dbf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'max_depth': 8,\n",
       " 'max_features': 20,\n",
       " 'min_samples_leaf': 30,\n",
       " 'min_samples_split': 40,\n",
       " 'n_estimators': 20}"
      ]
     },
     "execution_count": 142,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#随机森林\n",
    "#多个决策树组成，尽量使多个树之间是有差异的\n",
    "#使多个树之间是有差异 1.样本抽样， 2. 特征数\n",
    "#控制树过大（怕产生过拟合）---预剪枝\n",
    "#1. 最大深度（树的层数，不包含叶子层）\n",
    "#2. 分裂所需的最小样本数\n",
    "#3. 叶节点最小样本数\n",
    "#n_estimators  子模型的数量(树的个数)\n",
    "#max_features  节点分裂时参与判断的最大特征数\n",
    "#max_depth  最大深度\n",
    "#min_samples_split 分裂所需的最小样本数\n",
    "#min_samples_leaf  叶节点最小样本数\n",
    "#bootstrap 是否bootstrap对样本抽样  False：子模型的样本一致，子模型间强相关  True：默认值\n",
    "# param_grid = {'n_estimators': [20, 50, 100,300], 'max_features': [10,20,30,40,50,60],\"max_depth\":[4,6,8,10,12],\n",
    "#              \"min_samples_split\": [10,20,30,40],\"min_samples_leaf\": [5,10,20,30]},\n",
    "#为模型能正常创建，可以少设置几个参数选项，让其跑通代码\n",
    "param_grid = {'n_estimators': [20], 'max_features': [20],\"max_depth\":[6,8],\n",
    "             \"min_samples_split\": [40],\"min_samples_leaf\": [30]},\n",
    "#4 * 6 * 5 * 4 * 4 * 5 \n",
    "model = RandomForestClassifier()\n",
    "grid_search = GridSearchCV(model, param_grid, cv=5, scoring='roc_auc')\n",
    "result = grid_search.fit(X_train, y_train)\n",
    "result.best_params_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 147,
   "id": "04fd568b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.6247825978867363"
      ]
     },
     "execution_count": 147,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result.best_score_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 148,
   "id": "d5c30088",
   "metadata": {},
   "outputs": [],
   "source": [
    "pre = result.predict(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 149,
   "id": "273198c2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.0 0.0\n"
     ]
    }
   ],
   "source": [
    "#精准率和召回率 \n",
    "print(precision_score(y_test,pre),recall_score(y_test,pre))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 150,
   "id": "52b7a06d",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[21:32:01] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.5.1/src/learner.cc:1115: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'. Explicitly set eval_metric if you'd like to restore the old behavior.\n",
      "[21:32:02] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.5.1/src/learner.cc:1115: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'. Explicitly set eval_metric if you'd like to restore the old behavior.\n",
      "[21:32:04] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.5.1/src/learner.cc:1115: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'. Explicitly set eval_metric if you'd like to restore the old behavior.\n",
      "[21:32:05] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.5.1/src/learner.cc:1115: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'. Explicitly set eval_metric if you'd like to restore the old behavior.\n",
      "[21:32:08] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.5.1/src/learner.cc:1115: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'. Explicitly set eval_metric if you'd like to restore the old behavior.\n",
      "[21:32:09] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.5.1/src/learner.cc:1115: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'. Explicitly set eval_metric if you'd like to restore the old behavior.\n",
      "[21:32:11] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.5.1/src/learner.cc:1115: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'. Explicitly set eval_metric if you'd like to restore the old behavior.\n",
      "[21:32:13] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.5.1/src/learner.cc:1115: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'. Explicitly set eval_metric if you'd like to restore the old behavior.\n",
      "[21:32:14] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.5.1/src/learner.cc:1115: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'. Explicitly set eval_metric if you'd like to restore the old behavior.\n",
      "[21:32:16] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.5.1/src/learner.cc:1115: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'. Explicitly set eval_metric if you'd like to restore the old behavior.\n",
      "[21:32:17] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.5.1/src/learner.cc:1115: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'. Explicitly set eval_metric if you'd like to restore the old behavior.\n",
      "[21:32:18] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.5.1/src/learner.cc:1115: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'. Explicitly set eval_metric if you'd like to restore the old behavior.\n",
      "[21:32:20] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.5.1/src/learner.cc:1115: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'. Explicitly set eval_metric if you'd like to restore the old behavior.\n",
      "[21:32:21] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.5.1/src/learner.cc:1115: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'. Explicitly set eval_metric if you'd like to restore the old behavior.\n",
      "[21:32:23] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.5.1/src/learner.cc:1115: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'. Explicitly set eval_metric if you'd like to restore the old behavior.\n",
      "[21:32:25] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.5.1/src/learner.cc:1115: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'. Explicitly set eval_metric if you'd like to restore the old behavior.\n",
      "[21:32:27] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.5.1/src/learner.cc:1115: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'. Explicitly set eval_metric if you'd like to restore the old behavior.\n",
      "[21:32:30] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.5.1/src/learner.cc:1115: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'. Explicitly set eval_metric if you'd like to restore the old behavior.\n",
      "[21:32:32] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.5.1/src/learner.cc:1115: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'. Explicitly set eval_metric if you'd like to restore the old behavior.\n",
      "[21:32:36] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.5.1/src/learner.cc:1115: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'. Explicitly set eval_metric if you'd like to restore the old behavior.\n",
      "[21:32:38] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.5.1/src/learner.cc:1115: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'. Explicitly set eval_metric if you'd like to restore the old behavior.\n",
      "[21:32:41] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.5.1/src/learner.cc:1115: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'. Explicitly set eval_metric if you'd like to restore the old behavior.\n",
      "[21:32:45] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.5.1/src/learner.cc:1115: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'. Explicitly set eval_metric if you'd like to restore the old behavior.\n",
      "[21:32:48] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.5.1/src/learner.cc:1115: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'. Explicitly set eval_metric if you'd like to restore the old behavior.\n",
      "[21:32:51] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.5.1/src/learner.cc:1115: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'. Explicitly set eval_metric if you'd like to restore the old behavior.\n",
      "[21:32:55] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.5.1/src/learner.cc:1115: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'. Explicitly set eval_metric if you'd like to restore the old behavior.\n",
      "[21:32:58] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.5.1/src/learner.cc:1115: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'. Explicitly set eval_metric if you'd like to restore the old behavior.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[21:33:01] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.5.1/src/learner.cc:1115: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'. Explicitly set eval_metric if you'd like to restore the old behavior.\n",
      "[21:33:04] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.5.1/src/learner.cc:1115: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'. Explicitly set eval_metric if you'd like to restore the old behavior.\n",
      "[21:33:07] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.5.1/src/learner.cc:1115: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'. Explicitly set eval_metric if you'd like to restore the old behavior.\n",
      "[21:33:12] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.5.1/src/learner.cc:1115: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'. Explicitly set eval_metric if you'd like to restore the old behavior.\n",
      "[21:33:17] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.5.1/src/learner.cc:1115: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'. Explicitly set eval_metric if you'd like to restore the old behavior.\n",
      "[21:33:22] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.5.1/src/learner.cc:1115: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'. Explicitly set eval_metric if you'd like to restore the old behavior.\n",
      "[21:33:27] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.5.1/src/learner.cc:1115: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'. Explicitly set eval_metric if you'd like to restore the old behavior.\n",
      "[21:33:32] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.5.1/src/learner.cc:1115: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'. Explicitly set eval_metric if you'd like to restore the old behavior.\n",
      "[21:33:38] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.5.1/src/learner.cc:1115: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'. Explicitly set eval_metric if you'd like to restore the old behavior.\n",
      "[21:33:44] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.5.1/src/learner.cc:1115: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'. Explicitly set eval_metric if you'd like to restore the old behavior.\n",
      "[21:33:49] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.5.1/src/learner.cc:1115: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'. Explicitly set eval_metric if you'd like to restore the old behavior.\n",
      "[21:33:55] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.5.1/src/learner.cc:1115: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'. Explicitly set eval_metric if you'd like to restore the old behavior.\n",
      "[21:34:00] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.5.1/src/learner.cc:1115: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'. Explicitly set eval_metric if you'd like to restore the old behavior.\n",
      "[21:34:06] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.5.1/src/learner.cc:1115: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'. Explicitly set eval_metric if you'd like to restore the old behavior.\n",
      "[21:34:12] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.5.1/src/learner.cc:1115: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'. Explicitly set eval_metric if you'd like to restore the old behavior.\n",
      "[21:34:18] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.5.1/src/learner.cc:1115: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'. Explicitly set eval_metric if you'd like to restore the old behavior.\n",
      "[21:34:24] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.5.1/src/learner.cc:1115: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'. Explicitly set eval_metric if you'd like to restore the old behavior.\n",
      "[21:34:29] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.5.1/src/learner.cc:1115: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'. Explicitly set eval_metric if you'd like to restore the old behavior.\n",
      "[21:34:35] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.5.1/src/learner.cc:1115: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'. Explicitly set eval_metric if you'd like to restore the old behavior.\n",
      "[21:34:49] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.5.1/src/learner.cc:1115: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'. Explicitly set eval_metric if you'd like to restore the old behavior.\n",
      "[21:35:03] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.5.1/src/learner.cc:1115: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'. Explicitly set eval_metric if you'd like to restore the old behavior.\n"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32mC:\\Users\\ADMINI~1\\AppData\\Local\\Temp/ipykernel_560/1026773306.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     19\u001b[0m \u001b[0mmodel\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mXGBClassifier\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     20\u001b[0m \u001b[0mgrid_search\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mGridSearchCV\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmodel\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mparam_grid\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcv\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mscoring\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'roc_auc'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 21\u001b[1;33m \u001b[0mtemp\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mgrid_search\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfit\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mX_train\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my_train\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     22\u001b[0m \u001b[0mtemp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mbest_params_\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\users\\administrator\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\sklearn\\model_selection\\_search.py\u001b[0m in \u001b[0;36mfit\u001b[1;34m(self, X, y, groups, **fit_params)\u001b[0m\n\u001b[0;32m    889\u001b[0m                 \u001b[1;32mreturn\u001b[0m \u001b[0mresults\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    890\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 891\u001b[1;33m             \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_run_search\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mevaluate_candidates\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    892\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    893\u001b[0m             \u001b[1;31m# multimetric is determined here because in the case of a callable\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\users\\administrator\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\sklearn\\model_selection\\_search.py\u001b[0m in \u001b[0;36m_run_search\u001b[1;34m(self, evaluate_candidates)\u001b[0m\n\u001b[0;32m   1390\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0m_run_search\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mevaluate_candidates\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1391\u001b[0m         \u001b[1;34m\"\"\"Search all candidates in param_grid\"\"\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1392\u001b[1;33m         \u001b[0mevaluate_candidates\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mParameterGrid\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mparam_grid\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1393\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1394\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\users\\administrator\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\sklearn\\model_selection\\_search.py\u001b[0m in \u001b[0;36mevaluate_candidates\u001b[1;34m(candidate_params, cv, more_results)\u001b[0m\n\u001b[0;32m    849\u001b[0m                     )\n\u001b[0;32m    850\u001b[0m                     for (cand_idx, parameters), (split_idx, (train, test)) in product(\n\u001b[1;32m--> 851\u001b[1;33m                         \u001b[0menumerate\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcandidate_params\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0menumerate\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcv\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msplit\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mX\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mgroups\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    852\u001b[0m                     )\n\u001b[0;32m    853\u001b[0m                 )\n",
      "\u001b[1;32mc:\\users\\administrator\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\joblib\\parallel.py\u001b[0m in \u001b[0;36m__call__\u001b[1;34m(self, iterable)\u001b[0m\n\u001b[0;32m   1042\u001b[0m                 \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_iterating\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_original_iterator\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1043\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1044\u001b[1;33m             \u001b[1;32mwhile\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdispatch_one_batch\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0miterator\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1045\u001b[0m                 \u001b[1;32mpass\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1046\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\users\\administrator\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\joblib\\parallel.py\u001b[0m in \u001b[0;36mdispatch_one_batch\u001b[1;34m(self, iterator)\u001b[0m\n\u001b[0;32m    857\u001b[0m                 \u001b[1;32mreturn\u001b[0m \u001b[1;32mFalse\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    858\u001b[0m             \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 859\u001b[1;33m                 \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_dispatch\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtasks\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    860\u001b[0m                 \u001b[1;32mreturn\u001b[0m \u001b[1;32mTrue\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    861\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\users\\administrator\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\joblib\\parallel.py\u001b[0m in \u001b[0;36m_dispatch\u001b[1;34m(self, batch)\u001b[0m\n\u001b[0;32m    775\u001b[0m         \u001b[1;32mwith\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_lock\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    776\u001b[0m             \u001b[0mjob_idx\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_jobs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 777\u001b[1;33m             \u001b[0mjob\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_backend\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mapply_async\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mbatch\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcallback\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mcb\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    778\u001b[0m             \u001b[1;31m# A job can complete so quickly than its callback is\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    779\u001b[0m             \u001b[1;31m# called before we get here, causing self._jobs to\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\users\\administrator\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\joblib\\_parallel_backends.py\u001b[0m in \u001b[0;36mapply_async\u001b[1;34m(self, func, callback)\u001b[0m\n\u001b[0;32m    206\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0mapply_async\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfunc\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcallback\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mNone\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    207\u001b[0m         \u001b[1;34m\"\"\"Schedule a func to be run\"\"\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 208\u001b[1;33m         \u001b[0mresult\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mImmediateResult\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfunc\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    209\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mcallback\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    210\u001b[0m             \u001b[0mcallback\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mresult\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\users\\administrator\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\joblib\\_parallel_backends.py\u001b[0m in \u001b[0;36m__init__\u001b[1;34m(self, batch)\u001b[0m\n\u001b[0;32m    570\u001b[0m         \u001b[1;31m# Don't delay the application, to avoid keeping the input\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    571\u001b[0m         \u001b[1;31m# arguments in memory\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 572\u001b[1;33m         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mresults\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mbatch\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    573\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    574\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0mget\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\users\\administrator\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\joblib\\parallel.py\u001b[0m in \u001b[0;36m__call__\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m    261\u001b[0m         \u001b[1;32mwith\u001b[0m \u001b[0mparallel_backend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_backend\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mn_jobs\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_n_jobs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    262\u001b[0m             return [func(*args, **kwargs)\n\u001b[1;32m--> 263\u001b[1;33m                     for func, args, kwargs in self.items]\n\u001b[0m\u001b[0;32m    264\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    265\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0m__reduce__\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\users\\administrator\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\joblib\\parallel.py\u001b[0m in \u001b[0;36m<listcomp>\u001b[1;34m(.0)\u001b[0m\n\u001b[0;32m    261\u001b[0m         \u001b[1;32mwith\u001b[0m \u001b[0mparallel_backend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_backend\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mn_jobs\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_n_jobs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    262\u001b[0m             return [func(*args, **kwargs)\n\u001b[1;32m--> 263\u001b[1;33m                     for func, args, kwargs in self.items]\n\u001b[0m\u001b[0;32m    264\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    265\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0m__reduce__\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\users\\administrator\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\sklearn\\utils\\fixes.py\u001b[0m in \u001b[0;36m__call__\u001b[1;34m(self, *args, **kwargs)\u001b[0m\n\u001b[0;32m    209\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0m__call__\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    210\u001b[0m         \u001b[1;32mwith\u001b[0m \u001b[0mconfig_context\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m**\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mconfig\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 211\u001b[1;33m             \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfunction\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    212\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    213\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\users\\administrator\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\sklearn\\model_selection\\_validation.py\u001b[0m in \u001b[0;36m_fit_and_score\u001b[1;34m(estimator, X, y, scorer, train, test, verbose, parameters, fit_params, return_train_score, return_parameters, return_n_test_samples, return_times, return_estimator, split_progress, candidate_progress, error_score)\u001b[0m\n\u001b[0;32m    679\u001b[0m             \u001b[0mestimator\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfit\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mX_train\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mfit_params\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    680\u001b[0m         \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 681\u001b[1;33m             \u001b[0mestimator\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfit\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mX_train\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my_train\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mfit_params\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    682\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    683\u001b[0m     \u001b[1;32mexcept\u001b[0m \u001b[0mException\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\users\\administrator\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\xgboost\\core.py\u001b[0m in \u001b[0;36minner_f\u001b[1;34m(*args, **kwargs)\u001b[0m\n\u001b[0;32m    504\u001b[0m         \u001b[1;32mfor\u001b[0m \u001b[0mk\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0marg\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mzip\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msig\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mparameters\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0margs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    505\u001b[0m             \u001b[0mkwargs\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mk\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0marg\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 506\u001b[1;33m         \u001b[1;32mreturn\u001b[0m \u001b[0mf\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    507\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    508\u001b[0m     \u001b[1;32mreturn\u001b[0m \u001b[0minner_f\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\users\\administrator\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\xgboost\\sklearn.py\u001b[0m in \u001b[0;36mfit\u001b[1;34m(self, X, y, sample_weight, base_margin, eval_set, eval_metric, early_stopping_rounds, verbose, xgb_model, sample_weight_eval_set, base_margin_eval_set, feature_weights, callbacks)\u001b[0m\n\u001b[0;32m   1259\u001b[0m             \u001b[0mverbose_eval\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mverbose\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1260\u001b[0m             \u001b[0mxgb_model\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mmodel\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1261\u001b[1;33m             \u001b[0mcallbacks\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mcallbacks\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1262\u001b[0m         )\n\u001b[0;32m   1263\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\users\\administrator\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\xgboost\\training.py\u001b[0m in \u001b[0;36mtrain\u001b[1;34m(params, dtrain, num_boost_round, evals, obj, feval, maximize, early_stopping_rounds, evals_result, verbose_eval, xgb_model, callbacks)\u001b[0m\n\u001b[0;32m    194\u001b[0m                           \u001b[0mevals_result\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mevals_result\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    195\u001b[0m                           \u001b[0mmaximize\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mmaximize\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 196\u001b[1;33m                           early_stopping_rounds=early_stopping_rounds)\n\u001b[0m\u001b[0;32m    197\u001b[0m     \u001b[1;32mreturn\u001b[0m \u001b[0mbst\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    198\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\users\\administrator\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\xgboost\\training.py\u001b[0m in \u001b[0;36m_train_internal\u001b[1;34m(params, dtrain, num_boost_round, evals, obj, feval, xgb_model, callbacks, evals_result, maximize, verbose_eval, early_stopping_rounds)\u001b[0m\n\u001b[0;32m     79\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mcallbacks\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mbefore_iteration\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mbst\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mi\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdtrain\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mevals\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     80\u001b[0m             \u001b[1;32mbreak\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 81\u001b[1;33m         \u001b[0mbst\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mupdate\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdtrain\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mi\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mobj\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     82\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mcallbacks\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mafter_iteration\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mbst\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mi\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdtrain\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mevals\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     83\u001b[0m             \u001b[1;32mbreak\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\users\\administrator\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\xgboost\\core.py\u001b[0m in \u001b[0;36mupdate\u001b[1;34m(self, dtrain, iteration, fobj)\u001b[0m\n\u001b[0;32m   1680\u001b[0m             _check_call(_LIB.XGBoosterUpdateOneIter(self.handle,\n\u001b[0;32m   1681\u001b[0m                                                     \u001b[0mctypes\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mc_int\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0miteration\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1682\u001b[1;33m                                                     dtrain.handle))\n\u001b[0m\u001b[0;32m   1683\u001b[0m         \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1684\u001b[0m             \u001b[0mpred\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpredict\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdtrain\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0moutput_margin\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mTrue\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtraining\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mTrue\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "#XGBoost(提升树) 弱学习器： cart树（分类回归树）\n",
    "#串行的（第二棵树是在第一棵树的基础上训练，训练的是第一棵树差值）\n",
    "#XGBoost \n",
    "#1. 损失函数\n",
    "#2. 一阶导数g, 二阶导数h (确定叶子节点权重，分支节点的特征)\n",
    "#3. 输出就是 y1 + y2 + y3 \n",
    "#调参\n",
    "#1 剪枝： 最大深度，分裂所需的最小样本数，叶节点最小样本数\n",
    "#2 损失函数\n",
    "#3 棵树\n",
    "#4 数据输入： 记录数据占全部训练集的比例，特征占全部特征的比例\n",
    "#n_estimatores 即决策树的个数\n",
    "#max_depth 树的深度，默认值为6，典型值3-10。\n",
    "#subsample 训练每棵树时，使用的数据占全部训练集的比例。默认值为1，典型值为0.5-1。\n",
    "#colsample_bytree 训练每棵树时，使用的特征占全部特征的比例。默认值为1，典型值为0.5-1。\n",
    "#objective 选定损失函数\n",
    "param_grid = {'n_estimators': [20, 50, 100,300],\"max_depth\":[4,6,8,10,12],\n",
    "             \"subsample\": [0.3,0.5,0.6,0.7,0.8],\"colsample_bytree\": [0.3,0.5,0.6,0.7,0.8]},\n",
    "model = XGBClassifier()\n",
    "grid_search = GridSearchCV(model, param_grid, cv=3, scoring='roc_auc')\n",
    "temp=grid_search.fit(X_train, y_train)\n",
    "temp.best_params_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c938b525",
   "metadata": {},
   "outputs": [],
   "source": [
    "pre = temp.predict(X_test)\n",
    "#精准率和召回率  \n",
    "print(precision_score(y_test,pre),recall_score(y_test,pre))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b2a0cd3f",
   "metadata": {},
   "source": [
    "### 优质模型保存"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6c0d94fd",
   "metadata": {},
   "outputs": [],
   "source": [
    "#优质模型保存\n",
    "from sklearn.externals import joblib\n",
    "#保存模型\n",
    "joblib.dump(temp,'model.model')\n",
    "\n",
    "#加载模型\n",
    "#clf=joblib.load('model.model')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "304.188px"
   },
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
